/*
	Name: 加分二叉树
	Author: There,hello
	Date: 27-10-19 9:23
	Description: Luogu P1040
	Algorithm: 区间DP
*/
#include<bits/stdc++.h>
using namespace std;
int n,f[50][50],root_[50][50];
void print(int l,int r){
    printf("%d ",root_[l][r]);
    if(l==r)return;
    if(root_[l][r]-1>=l)print(l,root_[l][r]-1);
    if(r>=root_[l][r]+1)print(root_[l][r]+1,r);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&f[i][i]);
        root_[i][i]=i;
        f[i][i-1]=1;
    }
    for(int len=2;len<=n;++len){
        for(int i=1;i+len-1<=n;++i){
            int j=i+len-1;
            for(int k=i;k<=j;++k){
                int val=f[i][k-1]*f[k+1][j]+f[k][k];
                if(val>f[i][j]){
                    f[i][j]=val;
                    root_[i][j]=k;
                }
            }
        }
    }
    printf("%d\n",f[1][n]);
    print(1,n);
}